package com.yuan.service.serviceImpl;

import com.yuan.bean.Book;
import com.yuan.mapper.BookInformationMapper;
import com.yuan.service.BookInformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookInformServiceInpl implements BookInformService {
    @Autowired
    BookInformationMapper bookInformationMapper;

    /*
    查询所有书籍
     */
    @Override
    public List<Book> selectAll() {
        List<Book> books = bookInformationMapper.selectAll();
        return books;
    }

//    通过类别查找书籍
    @Override
    public List<Book> selectAllByCategory(String category) {
        List<Book> books = bookInformationMapper.selectAllByCategory(category);
        return books;
    }

    /*
    通过条件模糊查询书籍
     */
    @Override
    public List<Book> selectAllByCondition(String category,String bookName,String author) {
        if(category!=null && category!=""){
            category="%"+category+"%";
        }
        if(bookName!=null&&bookName!=""){
            bookName="%"+bookName+"%";
        }
        if(author!=null&&author!=""){
            author="%"+author+"%";
        }
        List<Book> books = bookInformationMapper.selectByCondition(category, bookName, author);
        return books;
    }

    /*
    动态修改书籍信息
     */
    @Override
    public boolean updateBookInfo(Book book) {
        final int i = bookInformationMapper.updateBookInfo(book);
        if(i>0){
            return true;
        }else {
            return false;
        }
    }

    /*
    添加书籍
     */
    @Override
    public boolean insertBook(Book book) {
        if(book.getBookName()!=null && book.getBookName()!="") {
            final int i = bookInformationMapper.insertBook(book);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        }else{
            return false;
        }
    }

    //删除 书籍通过bookId
    @Override
    public boolean delByBookId(Integer bookId) {
        final int i = bookInformationMapper.delByBookId(bookId);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }

    //通过bookId查找书籍
    @Override
    public Book selectByBookId(Integer bookId) {
        final Book book = bookInformationMapper.selectAllByBookId(bookId);
        return book;
    }


}
